package com.globallogic.training.javase.basics;

import java.util.Scanner;

/**
* The console application printing out the day of the week for the given day (1..31),
* month (January, February, ..., December) and year.
* 
* E.g. for "10 October 2010" it should print Sunday
*
* @version 1.0
* @author Artem.Zeltinsh
**/
public class CalendarSeeker
{   
	private static final String WELCOME_MESSAGE
		= "# This is the console application printing out the day of the week for the given day (1..31),"
		+ "#  month (January, February, ..., December) and year";

	private static final String PROMPT_MESSAGE = "Please enter a date, (e.g. 10 October 2010): ";
	
	private static final int NUMBER_OF_DAYS_IN_A_YEAR = 365;
	
	private static final int NUMBER_OF_DAYS_IN_A_LEAP_YEAR = 366;
	
	private static final int BASE_YEAR = 2001;

	public static void main(String[] args)
	{
		System.out.println(WELCOME_MESSAGE);
		System.out.print(PROMPT_MESSAGE);
		
		Scanner in = new Scanner(System.in);

		String inputString = in.nextLine();
		String[] tokens = inputString.split("\\s");

		int inputDay = Integer.parseInt(tokens[0]);
		Month inputMonth = Month.valueOf(tokens[1].toUpperCase());
		int inputYear = Integer.parseInt(tokens[2]);
    
		int sumOfDays = inputDay;
		
		for (int y = inputYear; y < BASE_YEAR; y++)
		{
			if (isLeapYear(y)) sumOfDays -= NUMBER_OF_DAYS_IN_A_LEAP_YEAR;
			else sumOfDays -= NUMBER_OF_DAYS_IN_A_YEAR;
			sumOfDays = sumOfDays % 7 + 7;			
		}	
		
		for (int y = BASE_YEAR; y < inputYear; y++)
		{
			if (isLeapYear(y)) sumOfDays += NUMBER_OF_DAYS_IN_A_LEAP_YEAR;
			else sumOfDays += NUMBER_OF_DAYS_IN_A_YEAR;
		}

        for (Month month : Month.values())
        {
            if(month.compareTo(inputMonth) == 0)
            {
            	break;
            }
            sumOfDays += month.getNumberOfDays();
            if (isLeapYear(inputYear) && month.equals(Month.FEBRUARY))
            {
            	sumOfDays++;
            }
        }
        
        DayOfWeek resultWeekday = DayOfWeek.valueOf(sumOfDays);      
        System.out.println("is " + resultWeekday.toString());
    }
    
    private static boolean isLeapYear(int year)
    { 
        if(year % 400 == 0)
            return true;
        else if(year % 100 == 0)      
            return false;
        else if(year % 4 == 0)
            return true;
        else return false;       
    }

    enum DayOfWeek
    { 
        SUNDAY("Sunday"),
        MONDAY("Monday"),
        TUESDAY("Tuesday"),
        WEDNESDAY("Wednesday"),
        THURSDAY("Thursday"),
        FRIDAY("Friday"),
        SATURDAY("Saturday"); 

        private String label;
 
        DayOfWeek (String label)
        {
             this.label = label; 
        }

        public String toString()
        {
            return label;
        }

        public static DayOfWeek valueOf(int index)
        {
            return values()[index%7];
        }

    }

    enum Month
    {
        JANUARY(31),
        FEBRUARY(28),
        MARCH(31),
        APRIL(30),
        MAY(31),
        JUNE(30),
        JULY(31),
        AUGUST(31),
        SEPTEMBER(30),
        OCTOBER(31),
        NOVEMBER(30),
        DECEMBER(31);
        
        int numberOfDays;
        
        Month (int numberOfDays)
        {
        	this.numberOfDays = numberOfDays;
        }
        
        int getNumberOfDays()
        {
        	return numberOfDays;
        }
    }    
}

